package com.chief.openfeign.web;

import com.chief.openfeign.web.filter.TestClsFilter;
import com.chief.openfeign.web.interceptor.TestInterceptor;
import com.chief.openfeign.web.listener.TestListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private TestInterceptor testInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(testInterceptor).addPathPatterns("/**").order(1);
    }

    @Bean
    public ServletListenerRegistrationBean registrationListener() {
        ServletListenerRegistrationBean<TestListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>();
        listenerRegistrationBean.setListener(new TestListener());
        listenerRegistrationBean.setOrder(1);
        return listenerRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean registerFilter() {
        FilterRegistrationBean<TestClsFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new TestClsFilter());
        filterRegistrationBean.setOrder(0);
        filterRegistrationBean.setName("test-cls-filter");
        filterRegistrationBean.addUrlPatterns("/**");
        return filterRegistrationBean;
    }
}
